;(function($){
	var main = function(){
		var sc = {};
		
	
		function getCookie(c_name)
		{
			if (document.cookie.length>0)
			  {
			  c_start=document.cookie.indexOf(c_name + "=")
			  if (c_start!=-1)
			    { 
			    c_start=c_start + c_name.length+1 
			    c_end=document.cookie.indexOf(";",c_start)
			    if (c_end==-1) c_end=document.cookie.length
			    return unescape(document.cookie.substring(c_start,c_end))
			    } 
			  }
			return ""
		}
		
		function setCookie(c_name,value,expiredays)
		{
			var exdate=new Date()
			exdate.setDate(exdate.getDate()+expiredays)
			document.cookie=c_name+ "=" +escape(value)+
			((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
		}
		
		function delCookie(name)
		{
			var exdate = new Date();
			exdate.setTime(exdate.getTime() - 1);
			var cval=getCookie(name);
			if(cval!=null)
			document.cookie= name + "="+cval+";expires="+exdate.toGMTString();
		}

		var addToCart = function(shopid, goodsid, num, goodsname, spec, price){
			var cart = readCart();
			if(!cart || cart.shopid != shopid)
				cart = {"shopid": shopid, "dtls": []};
			var find = cart.dtls.findIndex(function(a){
				return a.goodsid === goodsid;
			});
			if(find >=0){
				one = cart.dtls[find];
				one.total += one.total / one.qty * num;
				one.qty += num;
				if(one.qty<=0)
					cart.dtls.splice(find, 1);
			}else{
				cart.dtls.push({"goodsid": goodsid, "qty": num, "total": num * price})
			}

			setCookie("shopcart", formatCart(cart), 3);
		}
			
		var modCartNum = function(shopid, goodsid, num, goodsname, spec, price){
			var cart = readCart();
			if(!cart || cart.shopid != shopid)
				cart = {"shopid": shopid, "dtls": []};
			var find = cart.dtls.findIndex(function(a){
				return a.goodsid === goodsid;
			});
			if(find >=0){
				one = cart.dtls[find];
				one.total = one.total / one.qty * num;
				one.qty = num;
				if(one.qty<=0)
					cart.dtls.splice(find, 1);
			}else{
				cart.dtls.push({"goodsid": goodsid, "qty": num, "total": num * price})
			}

			setCookie("shopcart", formatCart(cart), 3);		
		}
		
		var clearCart = function(shopid){
			delCookie("shopcart");
		}
		
		var readCart = function(){
			str = getCookie("shopcart");
			if(!str || str==="") return null;
			i = str.indexOf(":");
			sc.shopid = parseInt(str.slice(0, i));
			sc.dtls = [];
			if(i ==str.length-1) return sc; 
			str = str.substring(i+1, str.length-1);
			dtls = str.split("|");
			for(i=0; i<dtls.length;i++){
				objs = dtls[i].split(",");
				obj = {};
				obj.goodsid = parseInt(objs[0]);
				obj.qty = parseInt(objs[1]);
				obj.total = parseFloat(objs[2]);
				sc.dtls.push(obj);
			}
			return sc;
		}
		
		var formatCart = function(obj){
			str = obj.shopid +":";
			for(i=0;i<obj.dtls.length;i++){
				str = str + obj.dtls[i].goodsid + "," + obj.dtls[i].qty +"," + obj.dtls[i].total + "|";
			}
			return str;
		}
		
		return {
			addToCart: addToCart,
			modCartNum: modCartNum,
			clearCart: clearCart,
			readCart: readCart
		}
	}();
	
	window.addToCart = main.addToCart;
	window.modCartNum = main.modCartNum;
	window.clearCart = main.clearCart;
	window.readCart = main.readCart;
})(jQuery);		